﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.model;
using Peekaboo2008.common;
using Peekaboo2008.dao;

namespace Peekaboo2008.view
{
    public partial class FormSuaThongTinNhanVien : Form
    {
        ThanhVienModel thanhVienModel = new ThanhVienModel();
        FormQuanLyNhanVien formQuanLyNhanVien;
        string Id = "";
       public FormSuaThongTinNhanVien(ThanhVienModel _model,ref FormQuanLyNhanVien _formQuanLyNhanVien)
       {
           InitializeComponent();
           this.thanhVienModel = _model; 
           this.grbDoiMatKhau.Visible = false;
           this.formQuanLyNhanVien = _formQuanLyNhanVien;
           LoadChiNhanhValue();
           LoadVaiTroValue();
           LoadTinhTrangValue();
           LoadValue();
       }
       public FormSuaThongTinNhanVien()
       {
           InitializeComponent();
           this.grbMatKhauMacDinh.Visible =false;
           LoadChiNhanhValue();
           LoadVaiTroValue();
           LoadTinhTrangValue();
       }


       /***/
       // Lấy dữ liệu cho hàm khởi tạo có tham số
       /***/

        public void LoadValues()
        {
            Id = thanhVienModel.MaThanhVien;
            txtTenNhanVien.Text = thanhVienModel.HoTen;
            txtDiaChi.Text = thanhVienModel.DiaChi;
            txtDienThoai.Text = thanhVienModel.DienThoai;
            txtEmail.Text = thanhVienModel.Email;
            txtBangCap.Text = thanhVienModel.Bangcap;
            dateTimePicker1.Text = thanhVienModel.NgaySinh;
            sttLabel.Text = thanhVienModel.TenDangNhap;
            cbbChiNhanh.SelectedIndex = Int32.Parse(thanhVienModel.MaChiNhanh);
            cbbVaiTro.SelectedIndex = Int32.Parse(thanhVienModel.MaVaiTro);
            if (thanhVienModel.Equals("Block"))
            {
                cbbTinhTrang.SelectedIndex = 0;
            }
            else
            {
                cbbTinhTrang.SelectedIndex = 1;
            }
        }


        /***/
        // Lấy dữ liệu cho hàm khởi tạo không tham số
        /***/

        public void LoadValue()
        {
            Id = thanhVienModel.MaThanhVien;
            txtTenNhanVien.Text = thanhVienModel.HoTen;
            txtDiaChi.Text = thanhVienModel.DiaChi;
            txtDienThoai.Text = thanhVienModel.DienThoai;
            txtEmail.Text = thanhVienModel.Email;
            txtBangCap.Text = thanhVienModel.Bangcap;
            dateTimePicker1.Text = thanhVienModel.NgaySinh;
            sttLabel.Text = thanhVienModel.TenDangNhap;
            cbbChiNhanh.SelectedIndex = Int32.Parse(thanhVienModel.MaChiNhanh);
            cbbVaiTro.SelectedIndex = Int32.Parse(thanhVienModel.MaVaiTro);
            if (thanhVienModel.Equals("Block"))
            {
                cbbTinhTrang.SelectedIndex = 0;
            }
            else
            {
                cbbTinhTrang.SelectedIndex = 1;
            }
        }
 
        /***/
        // Lay du lieu de vo chi nhanh combobox
        /***/

        public void LoadChiNhanhValue()
        {
            cbbChiNhanh.DataSource = ChiNhanhDAO.getAll();
            cbbChiNhanh.DisplayMember = "Ten";
            cbbChiNhanh.ValueMember = "ID";
        }


        /***/
        // Lay du lieu de vo vai tro combobox
        /***/

        public void LoadVaiTroValue()
        {
            cbbVaiTro.DataSource = VaiTroDAO.getAll();
            cbbVaiTro.DisplayMember = "Ten";
            cbbVaiTro.ValueMember = "ID";
        }


        /***/
        // Lay du lieu de vo tinh trang combobox
        /***/

        public void LoadTinhTrangValue()
        {
            cbbTinhTrang.Items.Add("Block");
            cbbTinhTrang.Items.Add("Unblock");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void FormSuaThongTinNhanVien_FormClosed(object sender, FormClosedEventArgs e)
        {

            formQuanLyNhanVien.LoadValue();

        }

        private void btnSuaThongTin_Click(object sender, EventArgs e)
        {
           
            ThanhVienModel model = new ThanhVienModel();
            model.MaThanhVien = Id;
            model.HoTen = txtTenNhanVien.Text;
            model.DiaChi = txtDiaChi.Text;
            model.DienThoai = txtDienThoai.Text;
            model.Email = txtEmail.Text;
            model.Bangcap = txtBangCap.Text;
            model.NgaySinh = dateTimePicker1.Text;
            model.MaChiNhanh = cbbChiNhanh.SelectedValue.ToString() ;
            model.MaVaiTro =cbbVaiTro.SelectedValue.ToString()  ;
            if(cbbTinhTrang.SelectedIndex == 0)
            {
                model.TinhTrang = "0"; 
            }
            else
            {
                model.TinhTrang = "1";
            }
            if (ThanhVienDAO.UpdateInfo(model))
            {
                Console.WriteLine("Thành Công");
            }
        }
    }
}
